﻿// JAW Edit Version 1.10
// August 19 2007
// Alan To 
// modified by Toby Lawrence (Apr 16 2008)

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace JAW
{
	/// <summary>
	/// Represents a single data logging entry.
	/// </summary>
	public struct DataEntry
	{
		public int RowNumber;
		public double AFR, Lambda, RPM, VoltageIn, VoltageInTwo, SensorTemperature;

		/// <summary>
		/// Creates a new entry.
		/// </summary>
		/// <param name="rowNum">the entry number</param>
		/// <param name="afr">the AFR value</param>
		/// <param name="lambda">the ambda value</param>
		/// <param name="rpm">the RPM value</param>
		/// <param name="vIn">the Vin value</param>
		/// <param name="vInTwo">the Vin two value</param>
		/// <param name="sensorTemp">the sensor temperature value</param>
		public DataEntry(int rowNum, double afr, double lambda, double rpm, double vIn, double vInTwo, double sensorTemp)
		{
			RowNumber = rowNum;
			AFR = afr;
			Lambda = lambda;
			RPM = rpm;
			VoltageIn = vIn;
			VoltageInTwo = vInTwo;
			SensorTemperature = sensorTemp;
		}

		/// <summary>
		/// Generates a delimited strings representing the data in this entry.
		/// </summary>
		/// <param name="delimiter"></param>
		/// <returns></returns>
		public string ToDelimitedString(string delimiter)
		{
			// we use the US English culture to maintain proper decimal point formatting
			// so that our CSV files can be opened properly
			CultureInfo usCulture = CultureInfo.GetCultureInfo("en-US");

			return string.Format("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}",
				delimiter, AFR.ToString(usCulture), Lambda.ToString(usCulture), RPM.ToString(usCulture),
				VoltageIn.ToString(usCulture), VoltageInTwo.ToString(usCulture),
				SensorTemperature.ToString(usCulture));
		}
	}

	/// <summary>
	/// Holds datalogging values and allows easy enumeration.
	/// </summary>
	public class DataLogging : IEnumerable<DataEntry>
	{
		#region Fields

		private static readonly DataLogging instance = new DataLogging();

		private List<DataEntry> storedData;
		private int rowCounter;

		#endregion

		private DataLogging()
		{
			storedData = new List<DataEntry>();
			rowCounter = 0;
		}

		/// <summary>
		/// The singleton instance of the DataLogging class.
		/// </summary>
		public static DataLogging Instance
		{
			get { return instance; }
		}

		#region Methods

		/// <summary>
		/// Stores a new datalogging entry.
		/// </summary>
		/// <param name="afr">the AFR value</param>
		/// <param name="lambda">the lambda value</param>
		/// <param name="rpm">the RPM value</param>
		/// <param name="vIn">the Vin value</param>
		/// <param name="vInTwo">the Vin two value</param>
		/// <param name="sensorTemp">the sensor temperature value</param>
		public void StoreEntry(double afr, double lambda, double rpm, double vIn, double vInTwo, double sensorTemp)
		{
			storedData.Add(new DataEntry(rowCounter, afr, lambda, rpm, vIn, vInTwo, sensorTemp));
			rowCounter++;
		}

		/// <summary>
		/// Gets the last added datalogging entry.
		/// </summary>
		/// <returns>the latest entry</returns>
		public DataEntry GetLatestEntry()
		{
			return (storedData.Count > 0 ? storedData[rowCounter-1] : new DataEntry());
		}

		/// <summary>
		/// Clears all current datalogging entries.
		/// </summary>
		public void ClearData()
		{
			storedData.Clear();
			rowCounter = 0;
		}

		#endregion

		#region IEnumerable<DataEntry> Members

		/// <summary>
		/// Gets an enumerator for the datalogging entries.
		/// </summary>
		/// <returns>a enumerator for all the datalogging entries</returns>
		public IEnumerator<DataEntry> GetEnumerator()
		{
			return storedData.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		/// <summary>
		/// Gets an enumerator for the datalogging entries.
		/// </summary>
		/// <returns>a enumerator for all the datalogging entries</returns>
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		#endregion
	}
}
